    //Random Game Engine
    //Copyright (C) 2012  Nikolay Dionisov

    //This program is free software: you can redistribute it and/or modify
    //it under the terms of the GNU General Public License as published by
    //the Free Software Foundation, either version 3 of the License, or
    //(at your option) any later version.

    //This program is distributed in the hope that it will be useful,
    //but WITHOUT ANY WARRANTY; without even the implied warranty of
    //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    //GNU General Public License for more details.

    //You should have received a copy of the GNU General Public License
    //along with this program.  If not, see <http://www.gnu.org/licenses/>.

	//------------------------------------------------------------------------

	//FILE NAME: BaseFramework.h
	//Definition of the base framework.
	//Creating renderer, and making sure there is a window so it can "draw" somewhere.
	//For encapsulation purposes, all connections with the OS are done here.
#pragma once
#include "..\CoreInterfaces.h"
#include "..\..\Video\VideoInterfaces.h"

namespace re
{
	namespace core
	{
		template<class T>
		class BaseOSFramework : public IOSFramework<T>
		{
		public:
			BaseOSFramework() { g_framework = this; }
			~BaseOSFramework() { g_framework = NULL; }


			void VInitialize(AppSettings* settings, RER_TYPE renderer_type)
			{
				p_settings_ = settings;
				VCreateMainWindow();
				p_renderer_ = VCreateRenderer();
			}
			void VShutDown()
			{
				VDestroyMainWindow();
			}
			virtual void VRun() = 0;
			
			//--------------------------------------------------
			//Callbacks
			//--------------------------------------------------
			virtual void SetCallbackReceiver(T* receiver)
			{
				p_receiver_ = receiver;
			}
			virtual void SetCallbackCreateDevice(pointer_void_renderer clb)
			{
				createdevice_ = clb;
			}
			virtual void SetCallbackDestroyDevice(pointer_void_void clb)
			{
				destroydevice_ = clb;
			}
			virtual void SetCallbackLostDevice(pointer_void_void clb)
			{
				lostdevice_ = clb;
			}
			virtual void SetCallbackResetDevice(pointer_void_void clb)
			{
				resetdevice_ = clb;
			}
			virtual void SetCallbackUpdate(pointer_bool_void clb)
			{
				update_ = clb;
			}
			virtual void SetCallbackRender(pointer_void_void clb)
			{
				render_ = clb;
			}
			

			IRenderer* VGetRenderer()
			{
				return p_renderer_;
			}
		private:
			virtual IRenderer* VCreateRenderer() = 0;
			virtual void VCreateMainWindow() = 0;
			virtual void VDestroyMainWindow() = 0;

		protected:

			//Functions calling callbacks here. For easy use inside this class
			void CallbackCreateDevice()
			{
				(p_receiver_->*createdevice_)(p_renderer_);
			}
			void CallbackDestroyDevice()
			{
				(p_receiver_->*destroydevice_)();
			}
			void CallbackLostDevice()
			{
				(p_receiver_->*lostdevice_)();
			}
			void CallbackResetDevice()
			{
				(p_receiver_->*resetdevice_)();
			}
			bool CallbackUpdate()
			{
				return (p_receiver_->*update_)();
			}
			void CallbackRender()
			{
				(p_receiver_->*render_)();
			}
		public:
			AppSettings* p_settings_;
		private:
			//and now the functions
			T* p_receiver_;
			pointer_void_renderer createdevice_;
			pointer_void_void destroydevice_;
			pointer_void_void lostdevice_;
			pointer_void_void resetdevice_;
			pointer_bool_void update_;
			pointer_void_void render_;
			IRenderer* p_renderer_;
		protected:
			static BaseOSFramework<T>* g_framework;
		};
	}
}